﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
using System.Data.SqlClient;

namespace DAO
{
    public class GioiTinhDAO
    {
        public static DataTable GetAll()
        {
            DataTable dt = new DataTable();
            try
            {
                SqlConnection cn = DataProvider.Connect();
                string str = "exec sp_GioiTinh";
                SqlDataAdapter da = new SqlDataAdapter(str, cn);
                da.Fill(dt);
                cn.Close();
            }
            catch (Exception)
            { }
            return dt;
        }
        public static string GetByID(int ma)
        {
            string str = "";
            GioiTinh gt = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_GioiTinh_GetById", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maGioiTinh", ma));
            SqlDataReader dataReader = cmd.ExecuteReader();
            if (dataReader.Read())
            {
                gt = new GioiTinh();
                gt.MoTa = Convert.ToString(dataReader["MoTa"]);
                str = gt.MoTa;
            }
            cn.Close();
            return str;
        }

        public static int GetByTen(string ten)
        {
            GioiTinh gt = null;
            SqlConnection cn = DataProvider.Connect();
                  SqlCommand cmd = new SqlCommand("sp_GioiTinh_GetByTen", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@TenGioiTinh", ten));
            SqlDataReader dataReader = cmd.ExecuteReader();
            if (dataReader.Read())
            {
                gt = new GioiTinh();
                gt.MaLoaiGioiTinh = Convert.ToInt32(dataReader["MaLoaiGioiTinh"]);
                gt.MoTa = ten;
            }
            cn.Close();
            return gt.MaLoaiGioiTinh;
        }


        public static List<GioiTinh> GetALL()
        {
            List<GioiTinh> dsGioiTinh = new List<GioiTinh>();
            GioiTinh gt = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_GioiTinh_GetALL", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dataReader = cmd.ExecuteReader();
            while (dataReader.Read())
            {
                gt = new GioiTinh();
                gt.MaLoaiGioiTinh = Convert.ToInt32(dataReader["MaLoaiGioiTinh"]);
                gt.MoTa =Convert.ToString(dataReader["MoTa"]);
                dsGioiTinh.Add(gt);
            }
            cn.Close();
            return dsGioiTinh;
        }
    }
}
